//
//  RCDChatListCell.m
//  RCloudMessage
//
//  Created by Liv on 15/4/15.
//  Copyright (c) 2015年 RongCloud. All rights reserved.
//

#define HEXCOLOR(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#import "RCDChatListCell.h"

@implementation RCDChatListCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        _ivAva = [UIImageView new];
        _ivAva.hidden = YES;
//        _ivAva.contentMode =UIViewContentModeScaleToFill;
        _ivAvaone = [UIImageView new];
        
        _ivAvaImage = [UIImageView new];
       
        //UIViewContentModeScaleToFill
        //UIViewContentModeScaleAspectFit
        //UIViewContentModeScaleAspectFill
        _lblDetail = [UILabel new];
        [_lblDetail setFont:[UIFont systemFontOfSize:14.f]];
        [_lblDetail setTextColor:HEXCOLOR(0x8c8c8c)];
        _lblDetail.text = [NSString stringWithFormat:@"来自%@的好友请求",_userName];
        
        _lblName = [UILabel new];
        [_lblName setFont:[UIFont boldSystemFontOfSize:16.f]];
        [_lblName setTextColor:HEXCOLOR(0x252525)];
        _lblName.text = @"好友消息";
        
        _myBadgeLabel = [UILabel new];
        _myBadgeLabel.backgroundColor = [UIColor redColor];
        _myBadgeLabel.textColor = [UIColor redColor];
        _myBadgeLabel.layer.masksToBounds = YES;
        _myBadgeLabel.layer.cornerRadius = 4;
       
        
        _labelTime = [[UILabel alloc] init];
        _labelTime.backgroundColor = [UIColor clearColor];
        _labelTime.font = [UIFont systemFontOfSize:14];
        _labelTime.textColor = [UIColor lightGrayColor];
        _labelTime.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_ivAvaone];

        [self.contentView addSubview:_ivAva];
        [self.contentView addSubview:_ivAvaImage];

        [self.contentView addSubview:_lblDetail];
        [self.contentView addSubview:_lblName];
        [self.contentView addSubview:_labelTime];
        [self.contentView addSubview:_myBadgeLabel];
        _ivAva.translatesAutoresizingMaskIntoConstraints = NO;
        _lblName.translatesAutoresizingMaskIntoConstraints = NO;
        _lblDetail.translatesAutoresizingMaskIntoConstraints = NO;
        _labelTime.translatesAutoresizingMaskIntoConstraints = NO;
        _myBadgeLabel.translatesAutoresizingMaskIntoConstraints = NO;
        
        
        [_ivAvaone mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.bottom.equalTo(self).offset(-7.5);
            make.width.mas_equalTo(55);
            make.top.equalTo(self).offset(7.5);
        }];
        
        _ivAvaone.layer.masksToBounds = YES;
        _ivAvaone.layer.cornerRadius = 55/2;
        
  
        
        NSDictionary *_bindingViews = NSDictionaryOfVariableBindings(_ivAva,_lblName,_lblDetail,_labelTime,_myBadgeLabel);
        
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: @"V:|-11-[_labelTime(20)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelTime)]];

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_labelTime(200)]-11-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelTime)]];

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[_ivAva(width)]" options:0 metrics:@{@"width" :@([RCIM sharedRCIM].globalConversationPortraitSize.width)} views:NSDictionaryOfVariableBindings(_ivAva)]];
      
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[_ivAva(height)]" options:0 metrics:@{@"height" :@([RCIM sharedRCIM].globalConversationPortraitSize.height)} views:NSDictionaryOfVariableBindings(_ivAva)]];
       //角标位置
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_ivAva]-(-5)-[_myBadgeLabel(8)]" options:kNilOptions metrics:kNilOptions views:_bindingViews]];
        
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_lblName(18)]-[_lblDetail(18)]" options:kNilOptions metrics:kNilOptions views:_bindingViews]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_myBadgeLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:8.f]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_myBadgeLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_ivAva attribute:NSLayoutAttributeTop multiplier:1.0 constant:2.f]];
        if ([reuseIdentifier isEqualToString:@"friend"]) {
            [self addConstraint:[NSLayoutConstraint constraintWithItem:_lblName attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_ivAva attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
            
            [self addConstraint:[NSLayoutConstraint constraintWithItem:_lblName attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_ivAva attribute:NSLayoutAttributeRight multiplier:1.0 constant:8]];
        } else {
            [self addConstraint:[NSLayoutConstraint constraintWithItem:_lblName attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_ivAva attribute:NSLayoutAttributeTop multiplier:1.0 constant:2.f]];
            
            [self addConstraint:[NSLayoutConstraint constraintWithItem:_lblName attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_ivAva attribute:NSLayoutAttributeRight multiplier:1.0 constant:8]];
        }
        
        
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_lblDetail attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_lblName attribute:NSLayoutAttributeLeft multiplier:1.0 constant:1]];
        
    }
    return self;
}
-(void)setIvAvaImageType:(NSString *)ivAvaImageType{
    _ivAvaImageType= ivAvaImageType;
    [_ivAvaImage mas_makeConstraints:^(MASConstraintMaker *make) {
        if ([_ivAvaImageType isEqualToString:@"0"]) {
            
            make.left.equalTo(_ivAvaone).offset(5);
            make.height.mas_equalTo(50);
            make.width.mas_equalTo(50);
            make.top.equalTo(_ivAvaone).offset(-6);

        }else{
            
            make.left.equalTo(_ivAvaone).offset(2);
            make.height.mas_equalTo(50);
            make.width.mas_equalTo(50);
            make.top.equalTo(_ivAvaone).offset(-6);

        }
     
    }];
    
}
@end
